#!/bin/bash

##########################################################################################
#                                                                                        #
# This script populates a directory with TDAQ S/W. It is a precondition for making a RPM #
#                                                                                        #
#  Author: Markus Joos, CERN                                                             #
#                                                                                        #
##########################################################################################

if [[ -z "$CMAKE_PREFIX_PATH" ]]; then
    echo "CMAKE_PREFIX_PATH env variable is not defined. Set up CMake." 1>&2
    exit 1
fi

if [[ -z "$RELEASE_TAG" ]]; then
    echo "RELEASE_TAG env variable is not defined. Run setup_tags.sh." 1>&2
    exit 1
fi

ROOT_PATH=$CMAKE_PREFIX_PATH

#######################
#create the directories
#######################
rm -rf drivers_Wupper
mkdir -p drivers_Wupper/cmem_rcc
mkdir -p drivers_Wupper/DFDebug
mkdir -p drivers_Wupper/rcc_error
mkdir -p drivers_Wupper/wupper
mkdir -p drivers_Wupper/src
mkdir -p drivers_Wupper/drivers
mkdir -p drivers_Wupper/script
mkdir -p drivers_Wupper/lib64
mkdir -p drivers_Wupper/bin
mkdir -p drivers_Wupper/regmap


#############
#Header files
#############
cp -f $ROOT_PATH/cmem_rcc/cmem_rcc_drv.h                     drivers_Wupper/cmem_rcc
cp -f $ROOT_PATH/cmem_rcc/cmem_rcc_common.h                  drivers_Wupper/cmem_rcc
cp -f $ROOT_PATH/cmem_rcc/cmem_rcc.h                         drivers_Wupper/cmem_rcc


cp -f $ROOT_PATH/../rcc_error/rcc_error/rcc_error.h                      drivers_Wupper/rcc_error

cp -f $ROOT_PATH/../DFDebug/DFDebug/DFDebug.h                            drivers_Wupper/DFDebug
cp -f $ROOT_PATH/../DFDebug/DFDebug/GlobalDebugSettings.h                drivers_Wupper/DFDebug
cp -f $ROOT_PATH/../regmap/regmap/regmap-struct.h                        drivers_Wupper/regmap
cp -f $ROOT_PATH/../regmap/regmap/regmap-common.h                        drivers_Wupper/regmap
cp -f $ROOT_PATH/wupper/tdaq_drivers.h           drivers_Wupper/wupper
cp -f $ROOT_PATH/wupper/xvc_pcie_driver.h        drivers_Wupper/wupper
cp -f $ROOT_PATH/wupper/xvc_pcie_ioctl.h         drivers_Wupper/wupper
cp -f $ROOT_PATH/wupper/xvc_pcie_user_config.h   drivers_Wupper/wupper

cp -f $ROOT_PATH/wupper/wupper_common.h                      drivers_Wupper/wupper


###################
#Driver source code
###################
cp -f $ROOT_PATH/src/cmem_rcc.c                         drivers_Wupper/src
cp -f $ROOT_PATH/src/wupper.c                           drivers_Wupper/src


#############
#Driver tools
#############
cp -f ./Makefile_drivers                                              drivers_Wupper/src/Makefile
cp -f ./dkms.conf                                                     drivers_Wupper
cp -f ./drivers_wupper                                                drivers_Wupper/script
cp -f ./drivers_wupper_sd.service                                     drivers_Wupper/script


tar czf  ../drivers_Wupper-$RELEASE_TAG-src.tar.gz --directory drivers_Wupper .
